package com.geepi.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mysql.jdbc.PreparedStatement;

public class SearchDao {
	/**
	 * @param micro
	 * @return
	 * @throws SQLException
	 */
	// static String sql = "select * from Student where ? = ?";
	public static int getIdByName(String name) throws SQLException {
		int id = -1;
		try {
			Connection conn = Dao.getConnection();
			PreparedStatement ps = (PreparedStatement) conn
					.prepareStatement("select * from Student where name = ?");
			// ps.setString(1, "name");
			ps.setString(1, name);
			ResultSet rs = ps.executeQuery();
			// List<Student> list = new ArrayList();
			while (rs.next()) {
				id = rs.getInt("id");
			}
			Dao.close(rs, ps, conn);

		} catch (SQLException e) {
			e.printStackTrace();
		}

		return id;
	}

	/**
	 * @param id
	 * @return
	 * @throws SQLException
	 */
	public static String getNameById(int id) throws SQLException {
		Connection conn;
		String name = null;
		try {
			conn = Dao.getConnection();
			PreparedStatement ps = (PreparedStatement) conn
					.prepareStatement("select * from Student where id = ?");
			// ps.setString(1, "");
			ps.setInt(1, id);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				name = rs.getString("name");
			}
			Dao.close(rs, ps, conn);

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return name;
	}
}
